/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getOwnedMembers;

import java.util.List;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.PackageImport;
import org.eclipse.uml2.uml.Signal;

/**
 * Command to find a type with a given name.
 * 
 * <p>
 * The following attributes must be set for the command to complete
 * successfully:
 * </p>
 * <ul>
 * <li>Context
 * <li>Name
 * </ul>
 * 
 * <p>
 * The following attributes can be read to access the result of the command:
 * </p>
 * <ul>
 * <li>Signal - will be null if not found.
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class FindSignalCommand extends Command {

	private Namespace context;
	private String name;
	private Signal signal;

	public FindSignalCommand() {
	}

	public FindSignalCommand(final Namespace context, final String name) {
		setContext(context);
		setName(name);
	}

	public void setContext(final Namespace context) {
		this.context = context;
	}

	public void setName(final String name) {
		this.name = (name != null) ? name.trim() : null;
	}

	public Signal getSignal() {
		return signal;
	}

	@Override
	public void execute() {
		if (name == null || name.length() == 0)
			return; // nothing to do, keep result == null

		signal = searchModelUpwards(context);
		if (signal == null)
			signal = searchImportedModels(context.getModel());
	}

	private Signal searchModelUpwards(final Namespace start) {
		Namespace searchContext = start;
		while (searchContext != null) {
			Signal found = searchNamespace(searchContext);
			if (found != null)
				return found;
			if (searchContext.getOwner() instanceof Namespace)
				searchContext = (Namespace) searchContext.getOwner();
			else
				searchContext = null;
		}
		return null;
	}

	private Signal searchNamespace(final Namespace namespace) {
		// in UML2 1.1+ we could just try getOwnedMember(x) but this
		// is not available in earlier versions
		for (NamedElement member : getOwnedMembers(namespace)) {
			if (member instanceof Signal && name.equals(member.getName()))
				return (Signal) member;
		}
		return null;
	}

	private Signal searchImportedModels(final Model model) {
		for (PackageImport packageImport : getPackageImports(model)) {
			org.eclipse.uml2.uml.Package importedPackage = packageImport
					.getImportedPackage();
			if (importedPackage != null) {
				Signal signal = searchNamespaceDownwards(importedPackage);
				if (signal != null)
					return signal;
			}
		}
		return null;
	}

	private Signal searchNamespaceDownwards(final Namespace namespace) {
		Signal found = searchNamespace(namespace);
		if (found != null)
			return found;
		for (NamedElement member : getMembers(namespace)) {
			if (member instanceof Namespace) {
				found = searchNamespaceDownwards((Namespace) member);
				if (found != null)
					return found;
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	private List<NamedElement> getMembers(final Namespace namespace) {
		return (List<NamedElement>) namespace.getMembers();
	}

	@SuppressWarnings("unchecked")
	private List<PackageImport> getPackageImports(final Model model) {
		return (List<PackageImport>) model.getPackageImports();
	}

	@Override
	public void undo() {
		signal = null;
	}

	@Override
	public void redo() {
		this.execute();
	}

	@Override
	public void dispose() {
		context = null;
		name = null;
		signal = null;
	}
}
